class CommentsController < ApplicationController



  def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.create(comment_params)
    @comment.user_id = current_user.id
    @comment.username = current_user.username
    if @comment.save
      redirect_to article_path(@article)
    end
  end

  def show
    @article = Article.find(params[:id])
    @comment = Comment.all
  end

  def edit
    @blog = Blog.find(params[:blog_id])
    @comment = Comment.find(params[:id])
  end

  def update
    @blog = Blog.find(params[:blog_id])
    comment = Comment.find(params[:id])
    comment.update(comments_params)
    if comment.save
      redirect_to "/blogs/"
    else
      render "/"
    end
  end

  def destroy
    @article = Article.find(params[:article_id])
    @comment = @article.comments.find(params[:id])
    @comment.destroy
    redirect_to article_path(@article)
  end

  private
    def comment_params
      params.require(:comment).permit(:commenter, :body, :user_id, :username)
    end

end
